package com.yanqu.road.server.cmd.activity.langyabang;

import com.yanqu.road.entity.activity.langyabang.entity.LangYaTypePatrons;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaUserPatronsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.LangYaBangPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.LangYaBangProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.langyabang.CrossLangYaBangMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;
@Cmd(code = CrossProtocol.C_LANG_YA_BANG_GET_AND_UPDATE_DATA,desc = "玩家获取用户信息并上传最新门客")
public class CrossGetLangYaBangUserDataCmd extends CrossUnionCommand {

    private int activityId;

    private boolean isSync;

    private Map<Integer, LangYaTypePatrons> userPatronsInfos;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        LangYaBangProto.LangYaBangGetUserDataFromCrossReqMsg reqMsg = LangYaBangProto.LangYaBangGetUserDataFromCrossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        isSync = reqMsg.getIsSync();
        if (!reqMsg.getNewPatronsInfoList().isEmpty()){
            userPatronsInfos = LangYaBangPb.parseMsgToPatronsInfoMap(reqMsg.getNewPatronsInfoList());
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        LangYaBangProto.LangYaBangUserDataSyncRespMsg.Builder builder = CrossLangYaBangMgr.getUserSyncMsg(activityId, userId, groupId, userPatronsInfos,serverId);
        if (isSync) {
            if (builder.getRet() == 0) {
                LangYaBangProto.LangYaBangUserDataSyncMsg userData = builder.getUserData();
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_LANG_YA_BANG_USER_MAIN_DATA_SYNC, userData);
                MessageHelper.sendPacket(serverId, userId, pbMsg);
            }
        }else {
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_LANG_YA_BANG_USER_MAIN_DATA_REQ, builder);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

    @Override
    public void sendData() {



    }
}
